package com.eight.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.eight.pojo.TbOrder;
import com.eight.service.AliPayService;
import com.eight.vo.ComResult;
import com.eight.vo.OrderPayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

import static com.eight.config.AliPayConfig.*;
import static com.eight.config.AliPayConfig.sign_type;

@Service
public class AliPayServiceImpl implements AliPayService {

    @Override
    public boolean IsPay(OrderPayVo orderPayVo) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, app_id, merchant_private_key, "json", charset, alipay_public_key, sign_type); //获得初始化的AlipayClient
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();//创建API对应的request类
        //request.putOtherTextParam("app_auth_token", "201912BXXXXXX");//授权token，代调用必传
        request.setBizContent("{" +
                "\"out_trade_no\":\""+orderPayVo.getOrderNo()+"\"}"); //设置业务参数
        AlipayTradeQueryResponse response = alipayClient.execute(request);//通过alipayClient调用API，获得对应的response类
        System.out.print(response.getBody());
        String money = response.getBuyerPayAmount();
        if (!"0.00".equals(money)){
            return true;
        }else {
            System.out.println("支付失败了");
            return false;
        }
    }
}
